package cn.starrysky.hrm.controller;

import cn.starrysky.hrm.docment.CourseDoc;
import cn.starrysky.hrm.dto.CourseDto;
import cn.starrysky.hrm.query.CourseDocQuery;
import cn.starrysky.hrm.service.ICourseService;
import cn.starrysky.hrm.domain.Course;
import cn.starrysky.hrm.query.CourseQuery;
import cn.starrysky.hrm.util.AjaxResult;
import cn.starrysky.hrm.util.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;


    /**
     * 保存和修改公用的
     * @param dto  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping("/save")
    @PreAuthorize("hasAuthority('course:save')")
    public AjaxResult addOrUpdate(@RequestBody CourseDto dto){
        try {
            if( dto.getCourse().getId()!=null)
                courseService.updateCourse(dto);
            else
                courseService.insertCourse(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
    //上线
    @PostMapping("/onLine/{id}")
    @PreAuthorize("hasAuthority('course:onLine')")
    public AjaxResult onLine(@PathVariable("id") Long id){
        courseService.onLineOrOffLine(id,true);
        return AjaxResult.me();
    }
    @PostMapping("/offLine/{id}")
    public AjaxResult offLine(@PathVariable("id") Long id){
        courseService.onLineOrOffLine(id,false);
        return AjaxResult.me();
    }

    //获取用户
    @GetMapping("/{id}")
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('course:list')")
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        //高级查询 获取keword
        String keyword = query.getKeyword();
        Wrapper wrapper = new EntityWrapper<Course>();
        //查询条件
        wrapper.like("name", keyword).or().like("for_user", keyword);
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page,wrapper);
        return new PageList<Course>(page.getTotal(),page.getRecords());
    }

    /**
     * 全局索索
     * @param docQuery
     * @return
     */
    @PostMapping("/queryCourses")
    public AjaxResult queryCourses(@RequestBody CourseDocQuery docQuery)
    {
        PageList<CourseDoc> pageList=courseService.queryCourses(docQuery);
        return AjaxResult.me().setResultObj(pageList);
    }
}
